package com.relytosoft.mizar.analyze.face.services.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.TaskRequestDto;
import com.relytosoft.mizar.analyze.base.enums.DeleteStatusEnum;
import com.relytosoft.mizar.analyze.base.enums.TaskStatusEnum;
import com.relytosoft.mizar.analyze.base.enums.TaskTypeEnum;
import com.relytosoft.mizar.analyze.face.api.entity.AyFaceTaskSon;
import com.relytosoft.mizar.analyze.face.api.pojo.dto.AyFaceTaskSonDto;
import com.relytosoft.mizar.analyze.face.api.pojo.dto.AyTaskIdDto;
import com.relytosoft.mizar.analyze.face.api.pojo.query.AyFaceTaskSonQuery;
import com.relytosoft.mizar.analyze.face.api.pojo.vo.AyCameraVo;
import com.relytosoft.mizar.analyze.face.services.service.AyFaceTaskService;
import com.relytosoft.mizar.analyze.face.services.service.AyFaceTaskSonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: zyc
 * @version: v1.0
 * @description: com.relytosoft.mizar.analyze.face.services.controller
 * @Date: 2022/5/13 10:00
 */
@Api(tags = {"人脸分析子任务"})
@RestController
@RequestMapping("/face/task/son")
public class AyFaceTaskSonController {

    @Resource
    private AyFaceTaskSonService ayFaceTaskSonService;

    @Resource
    private AyFaceTaskService ayFaceTaskService;


    @ApiOperation("创建人脸分析子任务")
    @PostMapping({"/insert"})
    public ResponseResult<Void> insert(@RequestBody AyFaceTaskSonDto ayFaceTaskSonDto) {
        AyFaceTaskSon ayFaceTaskSon = new AyFaceTaskSon();
        ayFaceTaskSon.setFaceTaskId(ayFaceTaskSonDto.getFaceTaskId());
        ayFaceTaskSon.setType(TaskTypeEnum.REAL_TIME.getValue());
        ayFaceTaskSon.setIsDeleted(DeleteStatusEnum.NOT_DELETED.getValue());
        ayFaceTaskSon.setStatus(TaskStatusEnum.CREATED.getValue());
        ayFaceTaskSon.setTaskName(ayFaceTaskService.getById(ayFaceTaskSonDto.getFaceTaskId()).getName());
        for (AyCameraVo camera : ayFaceTaskSonDto.getAyCameraVos()) {
            ayFaceTaskSon.setCameraId(camera.getCameraId());
            ayFaceTaskSon.setCameraName(camera.getCameraName());
            ayFaceTaskSon.setVideoUrl(camera.getVideoUrl());
            ayFaceTaskSon.setId(null);
            TaskRequestDto requestDto = ayFaceTaskSonService.createRequest(ayFaceTaskSon);
            boolean result = ayFaceTaskSonService.insertAyFaceTaskSon(ayFaceTaskSon);
            if (result) {
                ayFaceTaskSonService.insertFaceTaskSonKafkaOpMsg(requestDto);
            }
        }
        return ResponseResult.ok("创建人脸子任务成功");
    }


    @ApiOperation("删除人脸分析子任务")
    @PutMapping({"/delete"})
    public ResponseResult<Void> delete(@RequestBody AyTaskIdDto ayTaskIdDto) {
        boolean result = ayFaceTaskSonService.updateAyFaceTaskSon(ayTaskIdDto);
        if (result) {
            return ResponseResult.ok("删除人脸分析子任务成功");
        }
        return ResponseResult.fail("删除人脸分析子任务失败");
    }


    @ApiOperation("启动人脸分析子任务")
    @PostMapping({"/start"})
    public ResponseResult<Void> start(@RequestBody AyTaskIdDto ayTaskIdDto) {
        List<AyFaceTaskSon> ayFaceTaskSons = ayFaceTaskSonService.listByIds(ayTaskIdDto.getIds());
        for (AyFaceTaskSon taskSon : ayFaceTaskSons) {
            boolean result = ayFaceTaskSonService.startAyFaceTaskSon(ayTaskIdDto, taskSon);
            if (result) {
                TaskRequestDto requestDto = ayFaceTaskSonService.createRequest(taskSon);
                ayFaceTaskSonService.insertFaceTaskSonKafkaOpMsg(requestDto);
            }
        }
        return ResponseResult.ok("启动人脸分析子任务成功");

    }

    @ApiOperation("停止人脸分析子任务")
    @PostMapping({"/stop"})
    public ResponseResult<Void> stop(@RequestBody AyTaskIdDto ayTaskIdDto) {
        List<AyFaceTaskSon> ayFaceTaskSons = ayFaceTaskSonService.listByIds(ayTaskIdDto.getIds());
        for (AyFaceTaskSon taskSon : ayFaceTaskSons) {
            boolean result = ayFaceTaskSonService.stopAyFaceTaskSon(ayTaskIdDto, taskSon);
            if (result) {
                ayFaceTaskSonService.deleteFaceTaskSonKafkaOpMsg(taskSon.getTaskId());
            }
        }
        return ResponseResult.ok("停止人脸分析子任务成功");
    }

    @ApiOperation("查询人脸分析子任务列表")
    @GetMapping({"/list"})
    public ResponseResult<Map<String, Object>> select(@RequestParam("queryBodys") String bodys, @RequestParam("pageQuery") String query) {
        AyFaceTaskSonQuery ayFaceTaskSonQuery = JSON.parseObject(bodys, AyFaceTaskSonQuery.class);
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        IPage<AyFaceTaskSon> page = ayFaceTaskSonService.selectAyFaceTaskSonPageList(pageQuery, ayFaceTaskSonQuery);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }
}
